package Test;

import java.lang.Thread.State;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import dal.database.DBUtil;

public class ItemOperator {
	static Connection con=null;
	//static Statement stUpdate=null;
	static Statement st=null;
	static ResultSet rs=null;
	static ItemOperator instance=null;
	
	private ItemOperator()
	{
		
	}
	
	static
	{
		try
		{
			con=DBUtil.getCon();
			st=con.createStatement();
			//stUpdate=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			instance=new ItemOperator();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static ItemOperator getInstance()
	{
		return instance;
	}
	
	
	public Integer getId() throws Exception
	{
		Integer id=0;
		String qry="select item_code from item_master order by item_code";
		try
		{
			rs=st.executeQuery(qry);
			if(!rs.next())
			{
				id=1;
			}
			else 
			{
				rs.last();
				id=rs.getInt(1);
				id++;
			}
		}
		catch(Exception e)
		{
			throw e;
		}
		return id;
	}
	
	
	
	public Boolean addItem(Item ob)throws SQLException
	{
		Boolean isAdded=false;
		String qry="insert into item_master values("+ob.getCode()+",'"+ob.getName()+"','"+ob.getPurchasePrize()+"','"+ob.getSellPrize()+"','"+ob.getStockistPrize()+"','"+ob.getNewConsumerPrize()+"','"+ob.getCommission()+"')";
		try
		{
			int res=st.executeUpdate(qry);
			if(res>0)
			{
				isAdded=true;
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return isAdded;
	}
	
	 
	 
	
	public Boolean updateItem(Item ob,Integer code)throws Exception
	{
		Boolean isUpdated=false;
		String qry="update item_master set name='"+ob.getName()+"',purchase_prize='"+ob.getPurchasePrize()+"',sell_prize='"+ob.getSellPrize()+"',stockist_prize='"+ob.getStockistPrize()+"',new_consumer_prize='"+ob.getNewConsumerPrize()+"',commission='"+ob.getCommission()+"' where item_code='"+code+"'";
		try
		{
			int res=st.executeUpdate(qry);
			if(res>0)
			{
				isUpdated=true;
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
		
		return isUpdated;
	}
	
	
	public ArrayList<Item> getItems() throws Exception 
	{
		ArrayList<Item> al=new ArrayList<Item>();
		String qry="select * from item_master";
		try
		{
			rs=st.executeQuery(qry);
			while(rs.next())
			{
				Item ob=new Item();
				ob.setCode(rs.getInt(1));
				ob.setName(rs.getString(2));
				ob.setPurchasePrize(rs.getDouble(3));
				ob.setSellPrize(rs.getDouble(4));
				ob.setStockistPrize((rs.getDouble(5)));
				ob.setNewConsumerPrize(rs.getDouble(6));
				ob.setCommission(rs.getDouble(7));
				
				al.add(ob);
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw e;
		}	
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
		return al;
	}
	
	public Item getItem(Integer code) throws Exception 
	{
		Item  item=new Item();
		String qry="select * from item_master where item_code="+code;
		try
		{
			rs=st.executeQuery(qry);
			if(rs.next())
			{
				item.setCode(rs.getInt(1));
				item.setName(rs.getString(2));
				item.setPurchasePrize(rs.getDouble(3));
				item.setSellPrize(rs.getDouble(4));
				item.setStockistPrize((rs.getDouble(5)));
				item.setNewConsumerPrize(rs.getDouble(6));
				item.setCommission(rs.getDouble(7));
				
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw e;
		}	
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
		return item;
	}
	
	public Item getItem(String itemName) throws Exception 
	{
		Item  item=new Item();
		String qry="select * from item_master where name='"+itemName+"'";
		try
		{
			rs=st.executeQuery(qry);
			if(rs.next())
			{
				item.setCode(rs.getInt(1));
				item.setName(rs.getString(2));
				item.setPurchasePrize(rs.getDouble(3));
				item.setSellPrize(rs.getDouble(4));
				item.setStockistPrize((rs.getDouble(5)));
				item.setNewConsumerPrize(rs.getDouble(6));
				item.setCommission(rs.getDouble(7));
				
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw e;
		}	
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
		return item;
	}
	
	
	public ArrayList<String> getItemLikeName(String itemName) throws Exception 
	{
		ArrayList<String> al=new ArrayList<String>();
		
		String qry="select distinct name from item_master where name like '"+itemName+"%'";
		//System.out.println("QRY=="+qry);
		try
		{
			rs=st.executeQuery(qry);
			while(rs.next())
			{
				al.add(rs.getString(1));
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw e;
		}	
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
		return al;
	}
	
	
	public Boolean deleteItem(Integer item_code) throws Exception
	{
		Boolean isDeleted=false;
		String qry="delete from item_master where item_code="+item_code;
		try
		{
			int res=st.executeUpdate(qry);
			if(res>0)
			{
				isDeleted=true;
				
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch(Exception e)
		{
			e.printStackTrace();	
			throw e;
		}
		return isDeleted;
	}
	
	
	public String getItemName(Integer code)
	{
		String name="";
		String qry="select name from item_master where item_code="+code;
		try
		{
			rs=st.executeQuery(qry);
			rs.next();
			name=rs.getString(1);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return name;
	}
	
	public Integer getItemCode(String itemName)
	{
		Integer code=0;
		String qry="select item_code from item_master where name='"+itemName+"'";
		System.out.println("QRY=="+qry);
		try
		{
			rs=st.executeQuery(qry);
			rs.next();
			code=rs.getInt(1);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return code;
	}
	
	/*public void check()
	{
		String qry="select * from item_master";
		try
		{
			rs=stUpdate.executeQuery(qry);
			rs.last();
			rs.moveToInsertRow();
			rs.updateInt(1,11);
			rs.updateString(2,"NTR");
			rs.insertRow();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	*/
	
}
